var loggedinCallback;

function verifyAuthentication(response, onLoggedinSuccess){
	if(isNaN(response) && response.indexOf("USER_IS_NOT_AUTHENTICATED") != -1){
		loggedinCallback = onLoggedinSuccess;
		$('#modal-login').modal('show');	
	}else{
		onLoggedinSuccess();
	}
}

function login(){
    
    var user_pass = $("#j_password").val();
    var user_name = $("#j_username").val();
    var rememberme = $("#rememberme").is(':checked');
    
    $.ajax({
          url: getCompletePath("j_spring_security_check"),
          data: { j_username: user_name , j_password: user_pass, _spring_security_remember_me: rememberme }, 
          type: "POST",
          beforeSend: function (xhr) {
             xhr.setRequestHeader("X-Ajax-call", "true");
          },
          success: function(result) {       
	          //if login is success, hide the login modal and
	          //re-execute the function which called the protected resource
	          //(#7 in the diagram flow)
	          if (result == "ok") {
	            $("#error-message").hide();            
	            $('#modal-login').modal('hide');
	            loggedinCallback();
	             
	            return true;
	          }else {           
	        	  $("#error-message").show();
	            //$("#ajax_login_error_"+ suffix).html('<span  class="alert display_b clear_b centeralign">Bad user/password</span>') ;
	            return false;           
	          }
    	  },
	      error: function(XMLHttpRequest, textStatus, errorThrown){
	          //$("#ajax_login_error_"+ suffix).html("Bad user/password") ;
	    	  $("#error-message").show();
	          return false; 
	      }
	});
}

